﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTTWebSite.PTTServiceReference1;

using System.Web.Security;


namespace PTTWebSite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            InfoServiceClient c = new InfoServiceClient();
  
            return View();
        }

        [HttpGet]
        public ActionResult StartTest()
        {
            return View();
        }


        
        public ActionResult StartTestM()
        {
            TestPTTWCF.TestInfoServiceClient c = new TestPTTWCF.TestInfoServiceClient();
            c.startTest();
            return View("Index");
        }

        public ActionResult ListStations(int? id)
        {
            PTTServiceReference1.StationListItem[] list = null;
            PTTServiceReference1.InfoServiceClient sref = new PTTServiceReference1.InfoServiceClient();            

            if (id.HasValue)
            {
                list = sref.ListStations(id.Value, "V");
            }
            else
            {
                list = null;
            }        

            ViewData["VID"] = id;

            PTTServiceReference1.InfoServiceClient c = new InfoServiceClient();


            ViewData["lines"] = new SelectList(c.GetLinesWithType());
            

            return View(list);
        }

        [HttpPost]
        public ActionResult LogIn(string UserName, string Password)
        {
            bool isOk = System.Web.Security.Membership.ValidateUser(UserName, Password);

            if (isOk)
            {
                System.Web.Security.FormsAuthentication.SetAuthCookie(UserName, false);
                return Redirect("Index");
            }

            ViewData["msg"] = "Invalid Username and/or Password";
            return View("SignIn");
        }

        public ActionResult SignIn()
        {
            return View();
        }

        public ActionResult SignOut()
        {
            System.Web.Security.FormsAuthentication.SignOut();

            return Redirect("Index");
        }

        public ActionResult Register()
        {
            return View();
        }


        public ActionResult RegisterPost(string UserName, string Password, string Email)
        {
            try
            {
                MembershipUser newUser = System.Web.Security.Membership.CreateUser(UserName, Password, Email);
                ViewData["msg"] = "You are registered, please log in";
                return View("SignIn");
            }
            catch (Exception e)
            {
                ViewData["errorMsg"] = e.Message;
                return View("Register");
            }
        }

        [Authorize]
        public ActionResult AddNewLine()
        {
            //string[] strList = new string[]{ "V", "B", "M"};

            //ViewData["VTypesForSelect"] = new SelectList(strList);
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult SaveNewLine()
        {

         //   string str = Request.Form[""];

           
            int nitems = 0;

            List<StationListItem> sitems = new List<StationListItem>();
            PTTServiceReference1.InfoServiceClient serviceClient = new PTTServiceReference1.InfoServiceClient();

            try
            {
                foreach (var item in Request.Form.Keys)
                {
                    string key = item.ToString();
                    if (key.StartsWith("input"))
                    {
                          String latStr = Request.Form["latitude" + nitems.ToString()].ToString();
                          latStr = latStr.Replace('.',',');   
                          String lonStr = Request.Form["longitude" + nitems.ToString()].ToString();
                          lonStr = lonStr.Replace('.',',');

                          if (latStr.Length > 5)
                          {
                              latStr = latStr.Substring(0, 5);
                          }
                          if (lonStr.Length > 5)
                          {
                              lonStr = lonStr.Substring(0, 5);
                          }

                         
                        sitems.Add(new StationListItem
                        {
                            StationName = Request.Form["input" + nitems.ToString()],
                          
                            Latitude = float.Parse(latStr),
                            Longiture = float.Parse(lonStr),
                            Snumber = nitems
                        });
                        nitems++;
                    }
                }
                int lineNumber = Convert.ToInt32(Request.Form["LineNumber"]);

                string vTypeSelected = Request.Form["CategoryId"];

                serviceClient.addNewLine(sitems.ToArray(), lineNumber, vTypeSelected);

                ViewData["AddLineMsg"] = "Line saved!";
                return View("AddNewLine");
            }
            catch (Exception e)
            {
                ViewData["AddLineMsg"] = "Wrong Input";
                return View("AddNewLine");
            }                 
        }
    }
}
